package io.renren.modules.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.pet.dao.PetTeamCommentDao;
import io.renren.modules.pet.dto.PetTeamCommentDTO;
import io.renren.modules.pet.entity.PetTeamCommentEntity;
import io.renren.modules.pet.service.PetTeamCommentService;
import io.renren.modules.pet.vo.PetTeamCommentVO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 宠物团队评论表
 *
 * @author zwp zwp
 * @since 1.0.0 2025-03-21
 */
@Service
public class PetTeamCommentServiceImpl extends CrudServiceImpl<PetTeamCommentDao, PetTeamCommentEntity, PetTeamCommentVO> implements PetTeamCommentService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<PetTeamCommentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<PetTeamCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void save(PetTeamCommentDTO dto) {
        PetTeamCommentEntity petTeamCommentEntity = ConvertUtils.sourceToTarget(dto, PetTeamCommentEntity.class);
        this.insert(petTeamCommentEntity);
    }

    @Override
    public void update(PetTeamCommentDTO dto) {
        PetTeamCommentEntity petTeamCommentEntity = ConvertUtils.sourceToTarget(dto, PetTeamCommentEntity.class);
        this.updateById(petTeamCommentEntity);
    }

    @Override
    public List<PetTeamCommentVO> getPetTeamComment(Long petTeamId) {
        QueryWrapper<PetTeamCommentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("pet_team_id", petTeamId);
        wrapper.orderByDesc("create_date");
        List<PetTeamCommentEntity> entityList = this.baseDao.selectList(wrapper);
        List<PetTeamCommentVO> voList = ConvertUtils.sourceToTarget(entityList, PetTeamCommentVO.class);
        voList.forEach(e -> {
            SysUserDTO userDto = sysUserService.get(e.getCreator());
            e.setCreatorName(userDto == null ? "" : userDto.getRealName());
        });
        return voList;
    }

}